﻿/****************************************
 *  copyright (c) 2008 leo shen
 *  
 *  all rights reserved         
 * 
 *  e-mail : syl.lucky@gmail.com
 * *************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace DotUpdater.Core.CurrentFilesGeters
{
    public class HttpCurrentFilesGeter : ICurrentFilesGeter
    {
        string m_httpAddress;

        public HttpCurrentFilesGeter(string httpAddress)
        {
            m_httpAddress = httpAddress;
        }

        #region ICurrentFilesGeter Members

        public void GetFileAsyc(App_File fileInfo)
        {
            WebClient wc = new WebClient();
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
            wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
            wc.DownloadDataAsync(new Uri(m_httpAddress + @"\" + fileInfo.Name));
        }

        void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            if (GetFileCompleted != null)
            {
                GetFileCompleted(this, e);
            }
        }

        void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (GetFileProgressChanged != null)
            {
                GetFileProgressChanged(this, e);
            }
        }

        public event System.Net.DownloadProgressChangedEventHandler GetFileProgressChanged;

        public event System.Net.DownloadDataCompletedEventHandler GetFileCompleted;

        #endregion
    }
}
